﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_Favorite.cs
* 创建标识：曾乃强 15158756652 2011-03-17 11:03:01
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_Favorite*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_Favorite
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_Favorite obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "Favorite ([ProductID],[UsersID])");
            sql.Append("values(@ProductID,@UsersID)");
            
            SqlParameter[] pra = {
                            new SqlParameter("@ProductID",obj.ProductID),
                            new SqlParameter("@UsersID",obj.UsersID)
                                 };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Update(Model.CNVP_Favorite obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "Favorite set [ProductID] = @ProductID,UsersID=@UsersID where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@ProductID",obj.ProductID),
                            new SqlParameter("@UsersID",obj.UsersID)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Delete(Model.CNVP_Favorite obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "Favorite where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public Model.CNVP_Favorite LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ProductID],[UsersID] from " + Prefix + "Favorite");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            Model.CNVP_Favorite cnvp_favorite = new Model.CNVP_Favorite();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_favorite.ProductID = dr["ProductID"] is DBNull ? 0 : (int)dr["ProductID"];
                cnvp_favorite.UsersID = dr["UsersID"] is DBNull ? 0 : (int)dr["UsersID"];
            }
             return cnvp_favorite;
        }


        /// <summary>
        /// 分页
        /// </summary>
        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string sql_Temp = "";

                string tFavorite = Prefix + "Favorite";
                string tProduct = Prefix + "Products";

                if (SqlCondition[0].value.ToString() != "")
                {
                    sql_Temp += "where " + tFavorite + ".UsersID=" + SqlCondition[0].value.ToString();
                } 
                string AllFields = tFavorite + ".ID," + tFavorite + ".ProductID," + tFavorite + ".UsersID," + tProduct + ".ProductName,"
                    + tProduct + ".MarketPrice," + tProduct + ".ImgProLoad," + tProduct + ".ProductClassID";
                string Condition = "" + Prefix + "Favorite " + " INNER JOIN " + tProduct + " ON " + tFavorite + ".ProductID=" + tProduct + ".ID " + sql_Temp;
                string IndexField = tFavorite + ".ID";
                string OrderFields = "order by " + tFavorite + ".ID Desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }

        #endregion
    }
}

